using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml.Serialization;
using umbraco.BusinessLogic;

namespace ClientTools.Server.Webservices.ConfigClasses
{
    [Serializable]
    [XmlRoot("ClientTools")]
    public class Config
    {
        [XmlAttribute("enabled")] 
        public bool Enabled { get; set; }
        
        [XmlElement(ElementName = "User")] 
        public List<User> Users { get; set; }

        public Provider UmbracoProvider { get; set; }
        public Provider DatabaseProvider { get; set; }

        public Config() 
        {
            Users = new List<User>();
        }

        public User GetUserByName(string userName)
        {
            foreach (var user in Users)
                if (user.Name == userName)
                    return user;
            return null;
        }

        private const string Configfilename = "ClientTools.config";
        private const string Cachekey = "ClientToolsConfiguration";

        private static Config _currentConfig;
        public static Config Current
        {
            get
            {
                if (_currentConfig == null)
                    _currentConfig = (Config)HttpRuntime.Cache[Cachekey] ?? LoadConfiguration();
                return _currentConfig;
                //if (_currentConfig == null)
                //    _currentConfig = LoadConfiguration();
                //return (Config)HttpRuntime.Cache[Cachekey] ?? LoadConfiguration();
            }
        }

        private static Config LoadConfiguration()
        {
            Log.Add(LogTypes.Debug, 0, "Loading ClientTools Configuration");

            var ser = new XmlSerializer(typeof(Config));
            var path = umbraco.GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "config" + Path.DirectorySeparatorChar + Configfilename;

            using (var fs = new FileStream(path, FileMode.Open))
            {
                try
                {
                    var retVal = (Config)ser.Deserialize(fs);
                    fs.Close();
                    //HttpRuntime.Cache.Insert(Cachekey, retVal, new CacheDependency(path));
                    return retVal;
                }
                catch (Exception ex)
                {
                    Log.Add(LogTypes.Error, 0, "Error loading ClientTools Configuration: " + ex.Message + ex.StackTrace);
                    return null;
                }
            }
        }

        public static void SaveConfiguration()
        {
            var ser = new XmlSerializer(typeof(Config));
            var path = umbraco.GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "config" + Path.DirectorySeparatorChar + Configfilename;

            using (var fs = new FileStream(path, FileMode.Create))
            {
                ser.Serialize(fs, _currentConfig);
                fs.Close();
            }
            //HttpRuntime.Cache[Cachekey] = Root;
        }
    }
}
